# -*- coding: utf-8 -*-

from server.controller import configuration
from server.controller import session
from server.controller import request
from server.controller import configurationDao
from server.controller import datetime
from server.controller import json

configurationObj = configurationDao()


@configuration.route('/getConfigurations')
def get_configurations():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    condition = {"creator": userSession['username']}

    limit = {"page": int(request.args.get('page')),
             "limit": int(request.args.get('limit'))}

    count, data = configurationObj.select_configurations(condition, limit)

    if count == 0:
        response['code'] = 1
        response['msg'] = "无数据"
        response['data'] = data
        return response

    response['code'] = 0
    response['msg'] = "查询成功"
    response['count'] = count
    response['data'] = data

    return response


@configuration.route('/addConfiguration', methods=['POST'])
def add_configuration():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    configurationInfo = {"name": request.form.get('name'),
                         "version": request.form.get('version'),
                         "project": request.form.get('project'),
                         "createDate": datetime.datetime.now().strftime('%Y-%m-%d'),
                         "creator": userSession['username'],
                         "equipmentList": json.loads(request.form.get('equipmentList')),
                         "describe": request.form.get('describe')}

    if configurationInfo['name'] == "" or configurationInfo['equipmentList'] == {}:
        response['code'] = 1
        response['msg'] = "新建配置需填写配置名称和配置表"
        response['data'] = data
        return response

    ret = configurationObj.add_configuration(configurationInfo)

    if not ret:
        response['code'] = 1
        response['msg'] = "新增失败"
        response['data'] = {}
        return response

    response['code'] = 0
    response['msg'] = "新增成功"
    response['data'] = data

    return response


@configuration.route('/updateConfiguration', methods=['POST'])
def update_configuration():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    configurationInfo = {"name": request.form.get('name'),
                         "version": request.form.get('version'),
                         "project": request.form.get('project'),
                         "createDate": datetime.datetime.now().strftime('%Y-%m-%d'),
                         "creator": userSession['username'],
                         "equipmentList": json.loads(request.form.get('equipmentList')),
                         "describe": request.form.get('describe')}

    ret = configurationObj.update_configuration(configurationInfo)

    if not ret:
        response['code'] = 1
        response['msg'] = "修改失败"
        response['data'] = {}
        return response

    response['code'] = 0
    response['msg'] = "修改成功"
    response['data'] = data

    return response


@configuration.route('/deleteConfiguration')
def delete_configuration():
    response = {}
    data = {}

    userSession = session.get('user')
    if userSession is None:
        response['code'] = 1
        response['msg'] = "用户未登录"
        response['data'] = data
        return response

    strId = request.args.get('batchId')
    ret = configurationObj.delete_configuration({"id": strId})

    if not ret:
        response['code'] = 1
        response['msg'] = "删除失败"
        response['data'] = data
        return response

    response['code'] = 0
    response['msg'] = "删除成功"
    response['data'] = data

    return response
